"""
Copyright (c) 2024 [XKW.Beijing]
All rights reserved.

Author: [tangxiaojun]
Email: [417281862@qq.com]
"""
from flask import Flask, g
import time

class TimingMiddleware:
    def __init__(self, app=None):
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.before_request(self.before_request)
        app.after_request(self.after_request)

    def before_request(self):
        g.start = time.time()

    def after_request(self, response):
        response_time = time.time() - g.start
        print(f"Request took: {response_time:.4f}s")
        return response